<template>
  <a-config-provider :locale="locale">
    <div id="app">
      <router-view />
      <vue-picture-viewer
        v-if="showPreviewImg"
        :img-data="previewImgs"
        :select-index="previewIndex"
        @close-viewer="showPreviewImg = false"
      />
    </div>
  </a-config-provider>
</template>
<script>
// Fontclass模式
import zhCN from 'ant-design-vue/lib/locale-provider/zh_CN'
import enquireScreen from '@/utils/device'
/** 常用图片预览创建组件 */
import VuePictureViewer from '@/components/VuePictureViewer/index'
export default {
  data() {
    return {
      locale: zhCN,
      showPreviewImg: false,
      previewIndex: 0,
      previewImgs: []
    }
  },
  components: {
    VuePictureViewer
  },
  created() {
    let that = this
    enquireScreen((deviceType) => {
      // tablet
      if (deviceType === 0) {
        that.$store.commit('TOGGLE_DEVICE', 'mobile')
        that.$store.dispatch('setSidebar', false)
      }
      // mobile
      else if (deviceType === 1) {
        that.$store.commit('TOGGLE_DEVICE', 'mobile')
        that.$store.dispatch('setSidebar', false)
      } else {
        that.$store.commit('TOGGLE_DEVICE', 'desktop')
        that.$store.dispatch('setSidebar', true)
      }
    })
    // 在页面加载时读取sessionStorage里的状态信息
    if (sessionStorage.getItem('store')) {
      this.$store.replaceState(Object.assign({}, this.$store.state, JSON.parse(sessionStorage.getItem('store'))))
    }

    // 在页面刷新时将vuex里的信息保存到sessionStorage里
    window.addEventListener('beforeunload', () => {
      sessionStorage.setItem('store', JSON.stringify(this.$store.state))
    })
  },
  mounted(){
    this.addBus()
  },
  methods:{
    addBus() {
      var self = this
      this.$bus.on('preview-image-bus', function(data) {
        console.log(data)
        self.previewIndex = data.index
        self.previewImgs = data.data
        self.showPreviewImg = true
      })
    },
  }
}
</script>
<style>
#app {
  height: 100%;
}
</style>